package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryEvaObjectListReq;
import com.sg.dto.base.app.req.BatchQueryEvaObjectReq;
import com.sg.dto.base.app.req.QueryEvaObjectDetailReq;
import com.sg.dto.base.app.req.QueryEvaObjectListReq;
import com.sg.entity.OmsEvaluationObject;
import com.sg.service.base.app.MOmsEvaluationObjectService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsEvaluationObjectMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsEvaluationObjectServiceImpl
        extends ServiceImpl<OmsEvaluationObjectMapper, OmsEvaluationObject>
        implements MOmsEvaluationObjectService {

    @Autowired
    OmsEvaluationObjectMapper omsEvaluationObjectMapper;

    /**
     * 3-2-06批量查评价对象[2482]
     * gen by moon at 10/15/2022, 5:44:09 PM
     */
    @Trace(operationName = "3-2-06批量查评价对象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationObject> batchQueryEvaObject(BatchQueryEvaObjectReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationObject> queryWrapper = new QueryWrapper<>();

            if (reqDto.getEvaObjectList() == null || reqDto.getEvaObjectList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("evaluation_portrait_id", reqDto.getEvaObjectList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationObject.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationObject.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationObject.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsEvaluationObject>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-05批量查评价对象列表（by评价画像）[2371]
     * gen by moon at 10/15/2022, 5:44:08 PM
     */
    @Trace(operationName = "3-2-05批量查评价对象列表（by评价画像）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationObject> batchQueryEvaObjectList(BatchQueryEvaObjectListReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationObject> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getEvaObjectList() == null || reqDto.getEvaObjectList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("evaluation_portrait_id", reqDto.getEvaObjectList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationObject.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationObject.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationObject.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsEvaluationObject>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-05新增评价对象[2350]
     * gen by moon at 10/15/2022, 5:44:07 PM
     */
    @Trace(operationName = "3-2-05新增评价对象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addEvaObject(OmsEvaluationObject omsEvaluationObject) {
        omsEvaluationObject.setEvaluationObjectId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsEvaluationObject.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        omsEvaluationObject.setIsUpdate("TRUE");
        omsEvaluationObject.setUpdateStatus("NEW");
        omsEvaluationObject.setArchivngType("NOT_ARCHIVE");
        omsEvaluationObject.setIsEditing("TRUE");
        omsEvaluationObject.setIsArchive("FALSE");
        omsEvaluationObject.setSubjectLifeCycle("EDITING");
        omsEvaluationObject.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationObject.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationObject.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationObject.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationObject.setIsValid("FALSE");
        omsEvaluationObject.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsEvaluationObject.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsEvaluationObject)) {
            return omsEvaluationObject.getEvaluationObjectId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-2-05查询评价对象详情[3463]
     * gen by moon at 4/24/2023, 12:54:18 AM
     */
    @Trace(operationName = "3-2-05查询评价对象详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsEvaluationObject queryEvaObjectDetail(QueryEvaObjectDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsEvaluationObject> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsEvaluationObject> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsEvaluationObject retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-05删除评价对象[4210]
     * gen by moon at 10/15/2022, 5:44:10 PM
     */
    @Trace(operationName = "3-2-05删除评价对象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteEvaObject(String evaluationObjectId) {

        return super.removeById(evaluationObjectId);
    }

    /**
     * M3-2-05构造单条评价对象验证数据[4354]
     * gen by moon at 10/15/2022, 5:44:10 PM
     */
    @Trace(operationName = "M3-2-05构造单条评价对象验证数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String buildSingleEvaObject(OmsEvaluationObject omsEvaluationObject) {
        omsEvaluationObject.setEvaluationObjectId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsEvaluationObject.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationObject.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsEvaluationObject != null && omsEvaluationObject.getSubjectLifeCycle() != null && omsEvaluationObject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationObject.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsEvaluationObject != null && omsEvaluationObject.getSubjectLifeCycle() != null && omsEvaluationObject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsEvaluationObject.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsEvaluationObject.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationObject.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsEvaluationObject != null && omsEvaluationObject.getSubjectLifeCycle() != null && omsEvaluationObject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationObject.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsEvaluationObject != null && omsEvaluationObject.getSubjectLifeCycle() != null && omsEvaluationObject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsEvaluationObject.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsEvaluationObject.setIsValid("FALSE");
        omsEvaluationObject.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsEvaluationObject.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsEvaluationObject)) {
            return omsEvaluationObject.getEvaluationObjectId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-2-05查询评价对象列表[7172]
     * gen by moon at 6/9/2023, 8:40:49 PM
     */
    @Trace(operationName = "3-2-05查询评价对象列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationObject> queryEvaObjectList(QueryEvaObjectListReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationObject> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationObject.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationObject.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationObject.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsEvaluationObject>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-05批量改评价对象[7173]
     * gen by moon at 6/9/2023, 8:40:50 PM
     */
    @Trace(operationName = "3-2-05批量改评价对象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateEvaObject(List<OmsEvaluationObject> omsEvaluationObject) {

        if (omsEvaluationObject == null || omsEvaluationObject.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsEvaluationObject);

    }
}
